package com.cloud.logger.zchain.zchainserver.mail.util;

import com.cloud.logger.zchain.zchainserver.mail.MailInfo;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.stream.Collectors;

/**
 * 浙江卓锐科技股份有限公司
 *
 * @author zhengyt
 * @create 2017/11/16
 * @description
 */
public class MailSendUtil {

    private static String HOST = "smtphz.qiye.163.com";
    private static String SOURCE_ADDRESS = "wucy@drore.com";
    private static String MAIL_USER_NAME = "wucy";
    private static String MAIL_PASSWORD = "Drore,wcy";
    private static String MAIL_TIME_OUT = "50000";
    private static String MAIL_AUTH = "true";

    private MailSendUtil(){

    }

    /**
     * 邮件发送方法调用方法要额外加异常捕获
     *
     * @param address
     * @param title
     * @param content
     */
    public static void sendMail(String address, String title, String content) {
        JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
        // 设定mail server
        senderImpl.setHost(HOST);
        // 建立邮件消息
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        // 设置收件人，寄件人 用数组发送多个邮件
        // String[] array = new String[] {"sun111@163.com","sun222@sohu.com"};
        // mailMessage.setTo(array);
        mailMessage.setTo(address);
        mailMessage.setFrom(SOURCE_ADDRESS);
        mailMessage.setSubject(title);
        mailMessage.setText(content);
        // 将MiniMultipart对象设置为邮件内容
        senderImpl.setUsername(MAIL_USER_NAME);
        senderImpl.setPassword(MAIL_PASSWORD);
        Properties prop = new Properties();
        //将这个参数设为true，让服务器进行认证,认证用户名和密码是否正确
        prop.put("mail.smtp.auth", MAIL_AUTH);
        prop.put("mail.smtp.timeout", MAIL_TIME_OUT);
        senderImpl.setJavaMailProperties(prop);
        // 发送邮件
        senderImpl.send(mailMessage);
    }

    /**
     * @ClassName: MailSendUtil
     * @author: wcy
     * @param:  * @param null
     * @return: 
     * @Decription: 
     * @CreateDate: 2017/12/13 
     */
    public static void sendHtmlMail(MailInfo mailInfo) throws Exception {

        Message message = getMessage(mailInfo);
        // MiniMultipart类是一个容器类，包含MimeBodyPart类型的对象
        Multipart mainPart = new MimeMultipart();
        // 创建一个包含HTML内容的MimeBodyPart
        BodyPart html = new MimeBodyPart();
        // 设置HTML内容
        html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
        mainPart.addBodyPart(html);
        // 将MiniMultipart对象设置为邮件内容
        message.setContent(mainPart);
        // 发送邮件
        Transport.send(message);
    }

    /**
     * @ClassName: MailSendUtil
     * @author: wcy
     * @param:  * @param null
     * @return:
     * @Decription:
     * @CreateDate: 2017/12/13
     */
    public static void sendHtmlMails(List<MailInfo> mailInfos) throws Exception {

    }

//    private static Message getMessage(MailInfo info) throws Exception{
//        final Properties p = System.getProperties() ;
//        p.setProperty("mail.smtp.host", HOST);
//        p.setProperty("mail.smtp.auth", MAIL_AUTH);
//        p.setProperty("mail.smtp.user", MAIL_USER_NAME);
//        p.setProperty("mail.smtp.pass", MAIL_PASSWORD);
//
//        // 根据邮件会话属性和密码验证器构造一个发送邮件的session
//        Session session = Session.getInstance(p, new Authenticator(){
//            protected PasswordAuthentication getPasswordAuthentication() {
//                return new PasswordAuthentication(p.getProperty("mail.smtp.user"),p.getProperty("mail.smtp.pass"));
//            }
//        });
//        session.setDebug(true);
//        Message message = new MimeMessage(session);
//        //消息发送的主题
//        message.setSubject(info.getSubject());
//        //接受消息的人
//        message.setReplyTo(InternetAddress.parse(info.getReplayAddress()));
//        //消息的发送者
//        message.setFrom(new InternetAddress(p.getProperty("mail.smtp.user"),"zchain"));
//        // 创建邮件的接收者地址，并设置到邮件消息中
//        message.setRecipient(Message.RecipientType.TO,new InternetAddress(info.getToAddress()));
//        // 消息发送的时间
//        message.setSentDate(new Date());
//
//        return message ;
//    }

    private static Message getMessage(MailInfo info) throws Exception{
        final Properties p = System.getProperties() ;
        p.setProperty("mail.smtp.host", HOST);
        p.setProperty("mail.smtp.auth", MAIL_AUTH);
        p.setProperty("mail.smtp.user", SOURCE_ADDRESS);
        p.setProperty("mail.smtp.pass", MAIL_PASSWORD);

        // 根据邮件会话属性和密码验证器构造一个发送邮件的session
        Session session = Session.getInstance(p, new Authenticator(){
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(p.getProperty("mail.smtp.user"),p.getProperty("mail.smtp.pass"));
            }
        });
        session.setDebug(true);
        Message message = new MimeMessage(session);
        //消息发送的主题
        message.setSubject(info.getSubject());
        //接受消息的人
        message.setReplyTo(InternetAddress.parse(SOURCE_ADDRESS));
        //消息的发送者
        message.setFrom(new InternetAddress(p.getProperty("mail.smtp.user")));

        String[] addrs = info.getToAddress().split(";");
        InternetAddress[] address = new InternetAddress[addrs.length];
        for(int i=0;i<addrs.length;i++){
            address[i] = new InternetAddress(addrs[i]);
        }
        // 创建邮件的接收者地址，并设置到邮件消息中
        message.setRecipients(Message.RecipientType.TO,address);
        // 消息发送的时间
        message.setSentDate(new Date());

        return message ;
    }
}